package com.work.tqyb.ui.activity;

import android.app.Activity;
import android.content.Intent;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.lifecycle.ViewModelProvider;

import com.work.tqyb.base.BaseActivity;
import com.work.tqyb.databinding.ActivityAirInfoBinding;
import com.work.tqyb.http.response.AirCityResponse;
import com.work.tqyb.vm.AirVM;

public class AirInfoActivity extends BaseActivity {
    private AirVM vm;
    private ActivityAirInfoBinding binding;


    private ActivityResultLauncher<Intent> getCity = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), info -> {
        if (info.getResultCode() == Activity.RESULT_OK) {
            AirCityResponse.CityInfo cityBean = (AirCityResponse.CityInfo) info.getData().getSerializableExtra("city");
            vm.setChoiceCity(cityBean);
            vm.airLive();

        }
    });

    @Override
    public void initData() {

        vm = new ViewModelProvider(this).get(AirVM.class);
        binding = ActivityAirInfoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        vm.setChoiceCity(new AirCityResponse.CityInfo("55", "哈尔滨市", "230100", "HEBS"));
        vm.airLive();
    }

    @Override
    public void onViewClick() {
        binding.ibnBack.setOnClickListener(v -> finish());
        binding.ibnAdd.setOnClickListener(v -> getCity.launch(new Intent(AirInfoActivity.this, AirCityListActivity.class)));
        binding.btn14Day.setOnClickListener(v -> {
            Intent intent = new Intent(AirInfoActivity.this,AirLook14DayActivity.class);
            intent.putExtra("id",vm.getChoiceCity().Id);
            startActivity(intent);

        });
        binding.btn24Hour.setOnClickListener(v -> {
            Intent intent = new Intent(AirInfoActivity.this,AirLook24HourActivity.class);
            intent.putExtra("id",vm.getChoiceCity().Id);
            startActivity(intent);
        });
    }

    @Override
    public void observe() {
        vm.getAirLiveLD().observe(this, info -> {
            binding.tvCity.setText(info.CityName);
            binding.tvCO.setText("一氧化碳，mg/m3:" + info.CO);
            binding.tvNO2.setText("二氧化氮，μg/m3:" + info.NO2);
            binding.tvO3.setText("臭氧，μg/m3:" + info.O3);
            binding.tvPM10.setText("可吸入颗粒物，μg/m3:" + info.PM10);
            binding.tvPM25.setText("细颗粒物，μg/m3:" + info.PM2_5);
            binding.tvSO2.setText("二氧化硫，μg/m3:" + info.SO2);
            binding.tvAQI.setText("空气质量指数:" + info.AQI);
            binding.tvQuality.setText("根据AQI指数，分为优（0 ~ 50）、良（51 ~ 100）、轻度污染（101 ~ 150）、中度污染（151 ~ 200）、重度污染（201 ~ 300）和严重污染（>300）六个级别。:" + info.Quality);
            binding.tvPrimaryPollutant.setText("首要污染物:" + info.PrimaryPollutant);
            binding.tvMeasure.setText("建议措施描述:" + info.Measure);
            binding.tvUnheathful.setText("健康指引描述:" + info.Unheathful);
        });
    }
}
